// import TabMenu from './data';

const app = getApp();
Component({
  data: {
    active: null,
    list: [
      {
        icon: 'https://pc.xnzx888.com/resources/upload/tabbar/2.png',
        activeIcon: 'https://pc.xnzx888.com/resources/upload/tabbar/1.png',
        text: '首页',
        url: '/pages/home/home',
      },
      {
        icon: 'https://pc.xnzx888.com/resources/upload/tabbar/5.png',
        activeIcon: 'https://pc.xnzx888.com/resources/upload/tabbar/5.png',
      },
      {
        icon: 'https://pc.xnzx888.com/resources/upload/tabbar/9.png',
        activeIcon: 'https://pc.xnzx888.com/resources/upload/tabbar/8.png',
        text: '我的',
        url: '/pages/my/my',
      },
   
    ],
    scrollTop: 0,
    totalNum: 0,
    
  },

  methods: {
    onChange(event) {
   
      const { detail } = event;
      const currentItem = this.data.list[detail];
      const url = currentItem.url;
      console.log(url,33);
      wx.switchTab({
        url,
        success: () => {
            console.log(detail);
            this.setData({
                active: detail
            });
            console.log(this.data.active);
        }
    });
    },
    init() {
      const page = getCurrentPages().pop();
      const route = page ? page.route.split('?')[0] : '';
  
      const active = this.data.list.findIndex((item) => {
          // 检查 item.url 是否存在
          if (item.url) {
              // 去掉 item.url 开头的斜杠
              const urlWithoutSlash = item.url.startsWith('/') ? item.url.substr(1) : item.url;
              // 返回比较结果
              return urlWithoutSlash === route;
          }
          // 如果 item.url 不存在，返回 false
          return false;
      });
  
      console.log('??', active);
      this.setData({
          active
      });
  }
  },

  lifetimes: {
    // 组件实例进入页面节点树时执行
    attached: function () {
      // ...
    },
    ready: function () {
      // 页面初次渲染完成
    },
    show: function () {
      const currentPage = getCurrentPages().pop();
      const currentUrl = `/${currentPage.route}`;
      const index = this.data.list.findIndex(item => item.url === currentUrl);
      if (index !== -1) {
        this.setData({
          active: index
        });
      }
    }
  },

  // 组件布局完成后执行
  attached: function () {
  
  }
});